/*************************************************************************
 * File Name   : info.c
 * Brief       :
 * Change Logs :
 * Date           Author       Notes
 * 2022\03\10     liqi         The first version.
*************************************************************************/

#include <rtthread.h>
#include "temp.h"
#include "info.h"
#include <stdio.h>
#include <string.h>
#include <time.h>

#define DBG_SECTION_NAME               "app.info"
// #define INFO_DEBUG
#ifdef INFO_DEBUG
    #define DBG_LEVEL                      DBG_LOG
#else
    #define DBG_LEVEL                      DBG_INFO
#endif /* INFO */

#include <rtdbg.h>
struct json_type info;
static char *json_str = RT_NULL;

char *get_info(void)
{
    return json_str;
}

static void info_entry(void *args)
{
    float temperature;
    tempInit();
    time_t t;
    srand((unsigned) time(&t));
    time_t rawtime;
    struct tm *tm_info;
    if (json_str == RT_NULL)
    {
        json_str = calloc(1, 512);
    }
    while (1)
    {
        time(&rawtime);
        tm_info = localtime(&rawtime);
        strftime(info.datetime, 32, "%Y-%m-%d %H:%M:%S", tm_info);
        info.data.id0 = temperature = TempCal();
        info.data.id1 = rand() % 50;
        info.data.id2 = rand() % 50;
        info.data.id3 = rand() % 50;
        info.data.id4 = rand() % 50;
        sprintf(json_str, JSON_FMT, info.datetime,
                info.data.id0, info.data.id1, info.data.id2,
                info.data.id3, info.data.id4);
        LOG_D("t: %d", (uint16_t)temperature);
        rt_thread_mdelay(500);
    }
}

int start_info(void)
{
    rt_thread_t tid;

    tid = rt_thread_create("INFO",
                           info_entry, RT_NULL,
                           1024, 18, 5);
    if (RT_NULL != tid)
    {
        rt_thread_startup(tid);
        return RT_EOK;
    }
    return -RT_ERROR;
}
INIT_APP_EXPORT(start_info);
